package presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;

import entity.EPatternRegister;
import entity.ERegister;

public class PGearLabel extends JLabel implements Observer {
	private static final long serialVersionUID = 1L;
	private String prefix, unit;
	private enum Gears
	{
		firstgear, secondgear, thirdgear, fourthgear, fifthgear, sixthgear, neutral, nogear;
	    
	    public static Gears toDatatype(String datatype)
	    {
	        try {
	            return valueOf(datatype.toLowerCase());
	        } 
	        catch (Exception ex) {
	            return nogear;
	        }
	    }
	}

	public PGearLabel(String prefix, String text, String unit, 
			int horizontalAlignment, Font font,  int width, int height) {
		super(prefix + text + unit, horizontalAlignment);
		this.setPreferredSize(new Dimension(width,height));
		this.prefix = prefix;
		this.unit = unit;
		this.setFont(font);
	}
	
	private String getGear(String gear) {
		switch (Gears.toDatatype(gear.replace(" ", "").toLowerCase())) {
		case firstgear:
			return "1";
		case secondgear:
			return "2";
		case thirdgear:
			return "3";
		case fourthgear:
			return "4";
		case fifthgear:
			return "5";
		case sixthgear:
			return "6";
		case neutral:
			return "N";
		case nogear:
			return "-";
		default:
			return "-";
		}
	}

	@Override
	public void update(Observable obs, Object arg) {
		if (arg instanceof String && obs instanceof EPatternRegister) {
			if (((ERegister) obs).getName().equalsIgnoreCase("Gear")) {
				String gear = ((EPatternRegister) obs).getTextValue();
				this.setText(prefix + getGear(gear) + unit);
			}
		}
	}
}